/**
 * 
 */

package com.kugou.game.sdk.ui.fragment;

import com.kugou.game.sdk.api.common.User;
import com.kugou.game.sdk.base.BaseSDKWorkerFragment;
import com.kugou.game.sdk.core.UserManager;
import com.kugou.game.sdk.interfaces.OnRequestListener;
import com.kugou.game.sdk.ui.widget.LoadingView;
import com.kugou.game.sdk.utils.Rx;

import android.app.ProgressDialog;
import android.content.Context;
import android.os.Bundle;
import android.os.Message;
import android.text.Html;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.TextView;

/**
 * 描述:绑定（修改）安全邮箱
 *
 * @author jerryliu
 * @since 2015年8月19日 下午3:04:30
 */
public class SecurityEmailResultFragment extends BaseSDKWorkerFragment {
    protected static final int MSG_BACK_RESEND_EMAIL = 0;

    protected static final int MSG_UI_RESEND_EMAIL_SUCCESS = 1;

    protected static final int MSG_UI_RESEND_EMAIL_FAILE = 2;

    private Context context;

    private TextView tvSuccessTip, tvHint;

    private Button btnConfirm;

    private LoadingView loadingView;

    private View layoutBindRusult;

    private User user;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        context = getActivity();
        user = UserManager.getInstance().getCurrentUserInternal();
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View view = inflater.inflate(Rx.layout.kg_fragment_security_email_result, null);
        initView(view);
        return view;
    }

    String checkcode = "";

    @Override
    public void onActivityCreated(Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        Bundle args = getArguments();
        if (args != null && args.containsKey("checkcode")) {
            checkcode = args.getString("checkcode");
        }
    }

    private void initView(View view) {
        loadingView = (LoadingView) view.findViewById(Rx.id.kg_loadingView);
        loadingView.setText("正在操作，请稍候…");

        layoutBindRusult = view.findViewById(Rx.id.kg_layout_bind_result);
        tvSuccessTip = (TextView) view.findViewById(Rx.id.kg_tv_result);
        String tip = "已向<font color=\"#249EF6\">x%</font>发送修改邮箱邮件\n请登录邮箱，点击链接修改。";
        tvSuccessTip.setText(Html.fromHtml(tip.replace("x%", user.getSecurityEmail())));
        String hint = "2.如果垃圾箱没有，点击<font color=\"#249EF6\">再发一次邮件</font>。";
        tvHint = (TextView) view.findViewById(Rx.id.kg_binded_security_email);
        tvHint.setText(Html.fromHtml(hint));
        tvHint.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                if (dialog == null) {
                    dialog = new ProgressDialog(context);
                    dialog.setMessage("正在操作，请稍候...");
                }
                dialog.show();
                sendEmptyBackgroundMessage(MSG_BACK_RESEND_EMAIL);

            }

        });
        btnConfirm = (Button) view.findViewById(Rx.id.kg_btn_back_to_game_or_recharge);
        btnConfirm.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                if (getActivity() != null && !getActivity().isFinishing()) {
                    getActivity().finish();
                }
            }
        });
    }

    ProgressDialog dialog;

    @Override
    public void handleBackgroundMessage(Message msg) {
        String username = user.getAccountName();
        String kgToken = user.getKugouToken();
        String email = user.getSecurityEmail();

        String loginEmail = user.getLoginEmail();
        String securityEmail = user.getSecurityEmail();
        final Message message = new Message();
        switch (msg.what) {
            case MSG_BACK_RESEND_EMAIL:
                if (!TextUtils.isEmpty(loginEmail) && !TextUtils.isEmpty(securityEmail)
                        && loginEmail.equalsIgnoreCase(securityEmail)) {
                    UserManager.getInstance().sendSecurityEmailCode(username, kgToken, email, 2,
                            new OnRequestListener() {
                                @Override
                                public void onSuccess() {
                                    message.what = MSG_UI_RESEND_EMAIL_SUCCESS;

                                }

                                @Override
                                public void onFailed(String errorMsg) {
                                    message.what = MSG_UI_RESEND_EMAIL_FAILE;
                                    message.obj = errorMsg;
                                }
                            });
                } else {
                    UserManager.getInstance().updateSecurityEmail(username, kgToken, securityEmail,
                            checkcode, new OnRequestListener() {
                                @Override
                                public void onSuccess() {
                                    message.what = MSG_UI_RESEND_EMAIL_SUCCESS;
                                }

                                @Override
                                public void onFailed(String errorMsg) {
                                    message.what = MSG_UI_RESEND_EMAIL_FAILE;
                                    message.obj = errorMsg;
                                }
                            });
                }
                break;
        }

        sendUiMessage(message);
    }

    @Override
    public void handleUiMessage(Message msg) {
        if (getActivity() == null || getActivity().isFinishing() || isDetached() || !isAdded()) {
            return;
        }

        if (dialog != null) {
            dialog.dismiss();
        }

        switch (msg.what) {
            case MSG_UI_RESEND_EMAIL_SUCCESS:
                showToast("已重新发送邮件，请查收。");
                break;
            case MSG_UI_RESEND_EMAIL_FAILE:
                if (msg.obj != null) {
                    showToast(msg.obj.toString());
                }
                break;
        }
    }
}
